//
//  DetailOfPageViewController.m
//  PetAPP
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "DetailOfPageViewController.h"
#import "NetworkingHandle.h"
#import "ModelOfDetailOfPage.h"
#import "DetailOfHeaderReusableView.h"
#import "ModelOfPost.h"
#import "DetailFirstCell.h"
#import "DetailOtherCell.h"
#import "OldDetailOfHeaderReusableView.h"

@interface DetailOfPageViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) ModelOfDetailOfPage *detailOfModel;

@property (nonatomic, retain) UICollectionView *collection;

@property (nonatomic, retain) NSMutableArray *postArr;

@property (nonatomic, retain) UITextField *textField;

@property (nonatomic, retain) UIImageView *imageViewOfLike;

@end

@implementation DetailOfPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self changeNavigationBar];
    [self handleData];
    [self createTextField];
    
}

- (void)createTextField {
    UIView *viewOfText = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 60)];
    [self.view addSubview:viewOfText];
    [viewOfText release];
    
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 70, 40)];
    [viewOfText addSubview:self.textField];
    [_textField release];
    self.textField.placeholder = @"   回复楼主";
    self.textField.layer.cornerRadius = 5;
    self.textField.layer.borderWidth = 1;
    self.textField.layer.borderColor = [UIColor grayColor].CGColor;
    
    
    self.imageViewOfLike = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 10, 40, 40)];
    [viewOfText addSubview:self.imageViewOfLike];
    [_imageViewOfLike release];
    self.imageViewOfLike.image = [UIImage imageNamed:@"dakongxin"];
    
    

}

- (void)changeNavigationBar {
    self.navigationItem.title = @"帖子详情";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor brownColor],
        NSFontAttributeName : [UIFont systemFontOfSize:22]};
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
//    [button setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

}

- (void)handleData {
    NSDictionary *tempDic = [self.URLdic objectForKey:@"dic1"];
    [NetworkingHandle handleWorkData:[tempDic objectForKey:@"url"] AndBody:[tempDic objectForKey:@"body"] value:[tempDic objectForKey:@"value"] handleBlock:^(id result) {
    
        NSDictionary *tempDic = [result objectForKey:@"threadinfo"];
        
        _detailOfModel = [[ModelOfDetailOfPage alloc] init];
        [_detailOfModel setValuesForKeysWithDictionary:tempDic];
        /** 用户宠物信息 */
        _detailOfModel.recommendArr = [result objectForKey:@"recommendarray"];
        
        if ([self.headerMark isEqualToString:@"1"]) {
            /** 处理评论数据 */
            self.postArr = [NSMutableArray array];
            NSArray *tempArr = [result objectForKey:@"postarray"];
            
            for (NSDictionary *dic in tempArr) {
                ModelOfPost *model = [[ModelOfPost alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.postArr addObject:model];
                [model release];
            }
            [self createSubViews];
        }else {
            NSDictionary *tempDic2 = [self.URLdic objectForKey:@"dic2"];
            
            [NetworkingHandle handleWorkData:[tempDic2 objectForKey:@"url"] AndBody:[tempDic2 objectForKey:@"body"] value:[tempDic2 objectForKey:@"value"] handleBlock:^(id result) {
                self.postArr = [NSMutableArray array];
                NSArray *tempArr = [result objectForKey:@"postarray"];
                
                for (NSDictionary *dic in tempArr) {
                    ModelOfPost *model = [[ModelOfPost alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.postArr addObject:model];
                    [model release];
                }
                [self.collection reloadData];
            }];
            [self createSubViews];
        }
        
    }];
    
}

- (void)createSubViews {
    
    CGFloat width = self.view.frame.size.width;
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    
    flowLayout.itemSize = CGSizeMake(width, 100);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(width, 750);
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 0, 30, 0);
    
    

    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 124) collectionViewLayout:flowLayout];
    self.collection.backgroundColor = [UIColor colorWithRed:0.8489 green:0.8489 blue:0.8489 alpha:1.0];
    self.collection.showsVerticalScrollIndicator = NO;
    
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [_collection release];
    
    
    
    [self.collection registerClass:[DetailOfHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfHeader"];
    [self.collection registerClass:[OldDetailOfHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfOldHeader"];
    
    
    [self.collection registerClass:[DetailFirstCell class] forCellWithReuseIdentifier:@"poolOfFirst"];
    [self.collection registerClass:[DetailOtherCell class] forCellWithReuseIdentifier:@"poolOfOther"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.postArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.item == 0) {
        DetailFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfFirst" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        [cell handleDataWithModel:self.postArr[self.postArr.count - 1 - indexPath.item] andNumber:self.postArr.count];
        
        
        return cell;
    }else{
         DetailOtherCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfOther" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
        [cell handleDataWithModel:self.postArr[self.postArr.count - 1 - indexPath.item]];
        
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    ModelOfPost *model = self.postArr[self.postArr.count - 1 - indexPath.item];
    CGRect contentRect = [[model.message[0] objectForKey:@"content"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 65, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    if (indexPath.item == 0) {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20 + 91 + 10);
    }else {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20 + 51 + 10);
    }
    
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.headerMark isEqualToString:@"1"]) {
        DetailOfHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfHeader" forIndexPath:indexPath];
        [headerView handleDataWithModel:self.detailOfModel];
        
        return headerView;
    }else {
        OldDetailOfHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfOldHeader" forIndexPath:indexPath];
        [headerView handleDataWithModel:self.detailOfModel];
        
        return headerView;
    }
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGRect messageRect = [self.detailOfModel.subject boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    CGRect contentRect = [self.detailOfModel.content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    
    
    CGFloat sum = 50 + 400 + messageRect.size.height + contentRect.size.height + 30 + 20 + 10 + 50 + 10 + 30 + 5;
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, sum);
}





- (void)dealloc {
    [_URLdic release];
    [_headerMark release];
    [_postArr release];
    [_textField release];
    [_imageViewOfLike release];
    [_collection release];
    [_detailOfModel release];
    [super dealloc];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
